package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import controller.Controller;

/**
 * Class used to build menus for the game display's frame
 */
public class MenuBuilder implements MenuManager {

	// members
	private static final char NEW_GAME_SHORTCUT = 'N';
	private static final char TWO_PLAYERS_SHORTCUT = 'T';
	private static final char SINGLE_PLAYER_SHORTCUT = 'I';
	private static final char PARTIAL_GAME_SHORTCUT = 'P';
	private static final char AUTOMATIC_GAME_SHORTCUT = 'A';
	private static final char SPECIAL_GAME_SHORTCUT = 'S';
	
	// public methods
		
	/**
	 * Builds a menu-bar for new game.
	 * Uses game-mode-manager object while pairing each button with a listener 
	 */
	@Override
	public JMenuBar buildNewGameMenu(Controller gameManager) {
		
		// create a new menu-bar object
		JMenuBar menuBar = new JMenuBar();
		
		
		// create a new menu object and set its title
		JMenu menu = new JMenu("New Game");
		menu.setMnemonic(NEW_GAME_SHORTCUT);
		
		JMenuItem item = null;
		
		// create Single Player item
		item = new JMenuItem("Two Players");
		item.setMnemonic(TWO_PLAYERS_SHORTCUT);
		
		final Controller setGameModes = gameManager;
		
		// create listener for New Game item
		ActionListener twoPlayerListener = new ActionListener() {			
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				// end current game
				setGameModes.endGame();
				
				// refresh system
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
				// set two players game
				setGameModes.setTwoPlayersGame();
				
				// start game on a separate thread
				Timer timer = new Timer();
				TimerTask task = new TimerTask() {
					
					@Override
					public void run() {						
						// start game
						setGameModes.startGame();
					}
				};
				timer.schedule(task, 0);
			}
		};
		
		// pair item with listener
		item.addActionListener(twoPlayerListener);
		// add item to menu
		menu.add(item);

		// create Single Player item		
		item = new JMenuItem("Single Player");
		item.setMnemonic(SINGLE_PLAYER_SHORTCUT);
		
		// create listener for New Game item
		ActionListener singlePlayerListener = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				// end current game
				setGameModes.endGame();
				
				// refresh system
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
				// set single player game
				setGameModes.setSinglePlayerGame();

				// start game on a separate thread
				Timer timer = new Timer();
				TimerTask task = new TimerTask() {
					
					@Override
					public void run() {						
						// start game
						setGameModes.startGame();
					}
				};
				timer.schedule(task, 0);
			}
		};
		
		// pair item with listener
		item.addActionListener(singlePlayerListener);
		// add item to menu
		menu.add(item);
		
		// create Automatic Game item
		item = new JMenuItem("Automatic Game");
		item.setMnemonic(AUTOMATIC_GAME_SHORTCUT);
		
		// create listener for New Game item
		ActionListener automaticGameListener = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				// end current game
				setGameModes.endGame();
				
				// refresh system
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
				// set automatic game
				setGameModes.setAutomaticGame();
				
				// start game on a separate thread
				Timer timer = new Timer();
				TimerTask task = new TimerTask() {
					
					@Override
					public void run() {						
						// start game
						setGameModes.startGame();
					}
				};
				timer.schedule(task, 0);
			}
		};
		
		// pair item with listener
		item.addActionListener(automaticGameListener);
		// add item to menu
		menu.add(item);
		
		// create Partial Game item
		item = new JMenuItem("Partial Game");
		item.setMnemonic(PARTIAL_GAME_SHORTCUT);
		
		// create listener for New Game item
		ActionListener partialGameListener = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				// end current game
				setGameModes.endGame();
				
				// refresh system
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
				// set partial game
				setGameModes.setPartialGameBoard();
			}
		};
		
		// pair item with listener
		item.addActionListener(partialGameListener);		
		// add item to menu
		menu.add(item);
		
		// create Special Game item
		item = new JMenuItem("Special Game");
		item.setMnemonic(SPECIAL_GAME_SHORTCUT);
		
		// create listener for New Game item
		ActionListener specialGameListener = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				// end current game
				setGameModes.endGame();
				
				// refresh system
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
				// set special game
				setGameModes.setSpecialGame();
				
				// start game on a separate thread
				Timer timer = new Timer();
				TimerTask task = new TimerTask() {
					
					@Override
					public void run() {						
						// start game
						setGameModes.startGame();
					}
				};
				timer.schedule(task, 0);

			}
		};
		
		// pair item with listener
		item.addActionListener(specialGameListener);		
		// add item to menu
		menu.add(item);
		
		// add menu to the menu bar
		menuBar.add(menu);
		
		return menuBar;
	}
}